package com.etc.controller;

import com.etc.entity.Type;
import com.etc.entity.Video;
import com.etc.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("UploadFile")
public class UploadFile {
    @Autowired
    VideoService service;
    @RequestMapping("regivter")
    public String regivter(@RequestParam(value = "videoName") String videoName,
                         @RequestParam(value = "typeId") String typeId,
                         @RequestParam(value = "videoImg") MultipartFile videoImg,
                         @RequestParam(value = "videoFile") MultipartFile videoFile){
        String videoImgName=UUID.randomUUID()+videoImg.getOriginalFilename();
        File imgfile = new File("E:\\video\\" + videoImgName);
        String videoFilename=UUID.randomUUID()+videoImg.getOriginalFilename();
        File file = new File("E:\\video\\" + videoFilename);
        try {
            videoImg.transferTo(imgfile);
            videoFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean b = service.addVideo(new Video(videoName,new Type(typeId,null),videoImgName,videoFilename));
        return "backstage/video/video_all";

    }
    @RequestMapping("regivters")
    public void regivters(@RequestParam(value = "myfile") MultipartFile[] myfiles){
        if (myfiles!=null && myfiles.length>0){
            for (MultipartFile myfile:myfiles){
                UUID uuid = UUID.randomUUID();
                String filename=uuid+myfile.getOriginalFilename();
                File file = new File("E:\\video\\" + filename);
                try {
                    myfile.transferTo(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
